package wms.service.goods;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import wms.dao.goods.GoodsMapper;
import wms.dao.store.StoreSetMapper;
import wms.entity.Goods;

@Service
public class GoodsServiceImpl implements GoodsService{
	
	@Resource
	private GoodsMapper goodsMapper;
	
	@Resource
	private StoreSetMapper storeSetMapper;
	
	@Override
	public List<Goods> listAllGoodsWithParam(String goodsName, String goodsCode, Integer type, Integer startPage, Integer pageSize) throws Exception {
		return goodsMapper.listAllGoodsWithParam(goodsName, goodsCode, type, startPage, pageSize);
	}

	@Override
	public Goods getGoods(String goodsCode) throws Exception {
		return goodsMapper.getGoods(goodsCode);
	}

	@Override
	public boolean modifyGoods(Goods goods, String operator) throws Exception {
		if (goods == null) 
			throw new RuntimeException();
		
		goods.setModifyBy(operator);
		Date date = new Date(System.currentTimeMillis());
		goods.setModifyDate(date);
		return goodsMapper.txModify(goods);
	}

	@Override
	public boolean deleteGoods(String goodsCode) throws Exception {
		if (getGoods(goodsCode) == null)
			throw new RuntimeException();
		
		//1.清空仓库
		//2.删除物资
		
		if (storeSetMapper.txDeleteStoreSet(goodsCode, null)) {
			return goodsMapper.txDelete(goodsCode);
		} 
		return false;
		
	}

	@Override
	public int getTotalCount(String goodsName, String goodsCode, Integer type) throws Exception {
		return goodsMapper.getTotalCount(goodsName, goodsCode, type);
	}

	@Override
	public List<Goods> listGoodsWithSupplyCode(String supplyCode) throws Exception {
		return goodsMapper.listGoodsWithSupplyCode(supplyCode);
	}

	@Override
	public List<Goods> listGoodsWithRepoCode(String repoCode) throws Exception {
		return goodsMapper.listGoodsWithRepoCode(repoCode);
	}

}
